package cn.dnui_xqh414.dnuifood_16110100414.fragment_adapter_and_vhoder;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import cn.dnui_xqh414.dnuifood_16110100414.R;
import cn.dnui_xqh414.dnuifood_16110100414.bean.AllshopBean;
import cn.dnui_xqh414.dnuifood_16110100414.ShopFoodListActivity;

public class rv_Adapter
        extends RecyclerView.
        Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<AllshopBean> list;
    private LayoutInflater inflater;

    public rv_Adapter(Context context){
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<AllshopBean> list){
        this.list = list;

        notifyDataSetChanged();   //十分重要
    }

    class shopViewHolder extends
            RecyclerView.ViewHolder {

        public View itemView;

        public TextView title,address,intro;
        public RatingBar ratingBar;
        public ImageView imageView;

        public shopViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tv_shop_title);
            ratingBar = itemView.findViewById(R.id.rt_bar);
            address = itemView.findViewById(R.id.tv_shop_address);
            intro = itemView.findViewById(R.id.tv_shop_intro);
            imageView = itemView.findViewById(R.id.img_profile_cshop);

            this.itemView = itemView;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder
    onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.rv_item,
                        parent ,false);
        return new shopViewHolder(v);    //形同于 shopVIewHolder hoder = new ~;
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final AllshopBean allshopBean = list.get(position);
        final shopViewHolder viewHolder = (shopViewHolder) holder;
        if (allshopBean == null) return;

        viewHolder.title.setText(allshopBean.getShopname());
        viewHolder.ratingBar.setRating(allshopBean.getLevel());
        if(allshopBean.getIntro().equals(""))
            viewHolder.intro.setText("这个商家真懒，没有写intro");
        else
        viewHolder.intro.setText(allshopBean.getIntro());
        viewHolder.address.setText(allshopBean.getAddress());

        String url =
                "http://172.24.10.175:8080/foodService/"+allshopBean.getPic();
        //TODO 将没有图片的地方换成默认图片  /*samuiyuki 11/14/2018*/
        Picasso.get()
                .load(url)
                .placeholder(R.drawable.ic_loading)
                .error(R.drawable.ic_load_fail_sfont)
                .fit()
                .centerCrop()
                .into(viewHolder.imageView);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                Context context = v.getContext();
                intent.setClass(context, ShopFoodListActivity.class);
                intent.putExtra("shopid", allshopBean.getShop_id());
                intent.putExtra("shopname", allshopBean.getShopname());
                intent.putExtra("shoppic", allshopBean.getPic());
                intent.putExtra("phonenum",allshopBean.getPhonenum());
                // TODO: 11/7/2018 启动到新的界面
                context.startActivity(intent);

            }
        });

    }


    @Override
    public int getItemCount() {
        if (list == null)
            return 0;
        return list.size();
    }

}
